package com.fs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;

import com.fs.entity.UserInfo;
import com.fs.utils.DBUtils;

public class UserInfoDao {

	public UserInfo login(String user_email,String user_password){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int user_id = 0;
		UserInfo UserInfo  = new UserInfo();
		String sql ="select * from tb_userinfo where user_email= ? and user_password =?";
		try {
			con= DBUtils.getConnection();//获取数据库连接
			ps = con.prepareStatement(sql);//执行预编译sql语句
			ps.setString(1, user_email);//设置参数
			ps.setString(2, user_password);
			rs = ps.executeQuery();
			if(rs.next()){
				UserInfo.setUser_id(rs.getInt("user_id"));
				UserInfo.setUser_email(rs.getString("user_email"));
				UserInfo.setType(String.valueOf(rs.getInt("type")));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}
		return UserInfo;
	}
	/**
	 * 判断用户名或者邮箱是否存在
	 * 
	 */
	public boolean IsexitEmailUsername(String user_email){
		Connection con =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		boolean temp =false;
		String sql="select * from tb_userinfo where user_email=?";
		try {
			 con= DBUtils.getConnection();//获取数据库连接
			 ps = con.prepareStatement(sql);//执行预编译的sql语句
			 ps.setString(1, user_email);
			 rs=ps.executeQuery();
			 while(rs.next()){
				 temp =true;
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtils.close(con, rs, null, ps);
		}
		return temp;
	}
	/**
	 * 保存用户注册信息
	 */
	public void save(String user_email,String user_password,String usertype){
		Connection con =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		String sql = "insert into tb_userinfo(user_email,user_password,register_date,type) values (?,?,?,?)";
				try {
					 con= DBUtils.getConnection();//获取数据库连接
					 ps = con.prepareStatement(sql);//执行预编译的sql语句
					 ps.setString(1, user_email);
					 ps.setString(2, user_password);
					 ps.setTimestamp(3, new Timestamp(new Date().getTime()));
					 ps.setString(4, usertype);
					 ps.executeUpdate();//执行更新操作
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					DBUtils.close(con, rs, null, ps);
				}
	}
	
}
